package com.healer.controller;

import com.alibaba.fastjson.JSON;
import com.healer.common.AssembleResponseMsg;
import com.healer.common.CookieUtils;
import com.healer.pojo.ReturnBody;
import com.healer.pojo.TbUser;
import com.healer.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.healer.common.CookieUtils.setCookieAndSession;

@RestController
public class TbUserController {
   @Autowired
    JedisPool jedisPool;
    @Autowired
    TbUserService tbUserService;

    @RequestMapping(value="/user",produces = "application/json;charset=utf-8")
    public String test(){
        List<TbUser> users = tbUserService.getUsers();
        users.forEach(e-> System.out.println(e.toString()));
        String s = JSON.toJSONString(users);//通过pojo的get方法实现的
        return s;
    }

    /**
     * 功能描述：查询用户
     * @param map
     * @return
     */
    @RequestMapping(value="/queryuser",produces = "application/json;charset=utf-8")
    public ReturnBody  queryUser(@RequestBody Map<String,Object> map,HttpServletRequest request, HttpServletResponse response, HttpSession session){
        Jedis jedis=jedisPool.getResource();
        System.out.println("[debug]==>fuck");
        int flag=tbUserService.queryUser(map);
        String name="08172968";
        Map<String,String> all=new HashMap<>();
        if(flag==1){
            if(map.get("userId").equals(name))
                all.put("token","admin");
            else
                all.put("token","editor");
            all.put("status","OK");
            setCookieAndSession(request,response,map,session,jedis);
            jedis.close();
            return (ReturnBody) new AssembleResponseMsg().success(all);
        }
        else{
            return (ReturnBody) new AssembleResponseMsg().failure(200,"error","用户名或密码错误");
        }
    }
    /**
     * 功能描述：查询用户列表
     * @param map
     * @return
     */
    @RequestMapping("/queryuserlist")
    public ReturnBody queryUserList(@RequestBody Map<String,Object> map){
        List<TbUser> tbUsers = tbUserService.queryUserList(map);
        return new AssembleResponseMsg().success(tbUsers);
    }

    /**
     * 功能描述：新增用户
     * @param map
     * @return
     */
    @RequestMapping("/adduser")
    public ReturnBody addUser(@RequestBody Map<String,Object> map){
          tbUserService.addUser(map);
          return new AssembleResponseMsg().success("OK");
    }

    /**
     * 功能描述：更改用户信息
     * @param map
     * @return
     */
    @RequestMapping("/updateuser")
    public ReturnBody updateUser(@RequestBody Map<String,Object> map){
        tbUserService.updateUser(map);
        return new AssembleResponseMsg().success("OK");
    }
    /**
     * 功能描述：更新用户密码
     * @param map
     * @return
     */
    @RequestMapping("/updateuserpassword")
    public ReturnBody updateUserPassword(@RequestBody Map<String,Object> map){
        tbUserService.updateUserpassword(map);
        return new AssembleResponseMsg().success("OK");
    }

    /**
     * 功能描述：删除用户
     * @param map
     * @return
     */
    @RequestMapping("/deleteuser")
    public ReturnBody deleteUser(@RequestBody Map<String,Object> map){
        tbUserService.deleteUser(map);
        return new AssembleResponseMsg().success("OK");
    }
      /**
     * 功能描述：检查用户Id
     * @param userId
     * @return
     */
    @RequestMapping("/inspectuserid")
    public ReturnBody inspectUserId(String userId){
        int i = tbUserService.inspectUserId(userId);
        System.out.println("检查uid是不是1个"+i);
        if(i==1)
            return (ReturnBody) new AssembleResponseMsg().failure(200,"error","用户Id已经存在");
        else
            return new AssembleResponseMsg().success("OK");

    }



}
